<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoSupplier extends Controller_BoBase {
	public $auth_required = TRUE;
	
	public function action_index()
	{
		$view = View::factory("pages/bo/home");
		$this->setTab($view);
		$this->baseInfo($view);
		$this->request->response = $view;
	}

	public function action_insertPage(){
		$supplier_category = ORM::factory("supplierCategory");
		$result_supplier_category = $supplier_category->where("status","=","A")->find_all();
		$view = View::factory("pages/bo/supplier_add_page");
		$areaArray = array("市政府","步行街","狮标","鸳鸯池","华侨医院","服装城");
		$view->set_global('areaArray', $areaArray);
		$view->set_global('result_supplier_category', $result_supplier_category);
		exit($view->render());
	}

	public function action_insert(){
		ORM::factory("supplier")->insert($_POST);
		Request::$instance->redirect("boSupplier");
	}

	public function action_updatePage(){
		$supplier = ORM::factory("supplier",$_GET["supid"]);
		$supplier_category = ORM::factory("supplierCategory");
		$result_supplier_category = $supplier_category->where("status","=","A")->find_all();
		$view = View::factory("pages/bo/supplier_edit_page");
		$areaArray = array("市政府","步行街","狮标","鸳鸯池","华侨医院","服装城");
		$view->set_global('areaArray', $areaArray);
		$view->result = $supplier;
		$view->result_supplier_category = $result_supplier_category;
		exit($view->render());
	}

	public function action_update(){
		ORM::factory("supplier",$_POST["id"])->update($_POST);
	}

	public function action_detail(){
		$supplier = ORM::factory("supplier",$_GET["supid"]);
		$supplier_category = ORM::factory("supplierCategory");
		$result_supplier_category = $supplier_category->where("status","=","A")->find_all();
		$view = View::factory("pages/bo/supplier_detail_page");
		$view->result = $supplier;
		$view->result_supplier_category = $result_supplier_category;
		exit($view->render());
	}


	public function action_show(){
		$view = View::factory("pages/bo/supplier_page");
		$this->setTab($view);
		$this->baseInfo($view);
		exit($view->render());
	}

	public function action_search(){
		$categoryId = $_GET["categoryType"];
		$searchText = $_GET["searchText"];

		$view = View::factory("pages/bo/supplier_page");
		$supplier_category = ORM::factory("supplierCategory");
		$result_supplier_category = $supplier_category->where("status","=","A")->find_all();


		$supplier = ORM::factory("supplier");
		$supplier = $supplier->where("status","<>","I");
		if ($categoryId != 0){
			$supplier = $supplier->and_where("sup_cate_id","=",$categoryId);
			$view->set_global('result_category_type',$categoryId);
		}

		if(!empty($searchText)){
			if (intval($searchText) > 0){
				$supplier = $supplier->and_where("id","=",$searchText);
			}else{
				$supplier = $supplier->and_where("name","like","%".$searchText."%");
			}
			$view->set_global('result_search_text',$searchText);
		}
		if(array_key_exists("page", $_GET)){
			$page = $_GET["page"];
		}
		$tmp = clone $supplier;
		$count = $tmp->count_all();

		$pagination = Pagination::factory(array(
	  		'total_items'    => $count,
		));

		$result_supplier = $supplier->order_by('status','DESC')->order_by('id','DESC')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();

		$view->set_global('result_supplier', $result_supplier);
		$view->set_global('result_supplier_category', $result_supplier_category);

		$view->tab = 'supplier';

		$page_links = $pagination->render();
		$view->set_global('page_links', $page_links);
		$currentPage = 1;
		if (array_key_exists("page", $_REQUEST)){
			$currentPage = $_REQUEST["page"];
		}
		$view->set_global('current_page', $currentPage);
		exit($view->render());
	}

	public function action_deleteAll(){
		$status = "I";
		$supplier = ORM::factory("supplier");
		$supplier->status = $status;
		$ids = $_POST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$supplier->or_where("id","=",$id);
			}
		}
		$result_supplier = $supplier->save_all();
		if($this->updateStatus($result_supplier, $status)){
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除商家类别成功";
			exit($view->render());
		}
	}
	
	public function action_approve(){
		return $this->updateSupplierStatus("A", "审核通过");
	}
	
	public function action_unApprove(){
		return $this->updateSupplierStatus("F", "审核失败");
	}
	
	public function action_openMap(){
		$view = View::factory("pages/bo/supplier_map_page");
		exit($view->render());
	}
	
	public function action_mapData($supid){
		$result = ORM::factory("supplier")->where("status","<>","I")->and_where("id","=",$supid)->find_all();
		$top_array = array();
		foreach ($result as $item){
			$array = array(	"supplier_id"=>$item->id,
							"supplier_name"=>urlencode($item->name),
							"supplier_address"=>urlencode($item->address),
							"supplier_map_address"=>urlencode($item->map_address),
							"longitude"=>$item->longitude,
							"latitude"=>$item->latitude);
			array_push($top_array, $array);
		}
		$json_result = json_encode($top_array);
		echo (urldecode($json_result));
	}
	
	public function action_markMap(){
		ORM::factory("supplier",$_REQUEST["supplierid"])->markMap($_REQUEST);
		Request::$instance->redirect("boSupplier");
	}
	
	public function action_supplierData(){
		$result = ORM::factory("supplier")->where("status","<>","I")->find_all();
		$top_array = array();
		foreach ($result as $item){
			$array = array(	"supplier_id"=>$item->id,
							"supplier_name"=>urlencode($item->displaySupplierName()));
			array_push($top_array, $array);
		}
		$json_result = json_encode($top_array);
		echo (urldecode($json_result));
	}
	
	private function updateSupplierStatus($status,$msg){
		$supplier = ORM::factory("supplier",$_REQUEST["supid"]);
		$supplier->status = $status;
		$supplier->save();
		$dish_category = ORM::factory("dishCategory")->where("supplierid","=",$_REQUEST["supid"])->and_where("status","<>","I");
		$dish_category->status = $status;
		$dish_category->save_all();
		$dish = ORM::factory("dish")->where("supplierid","=",$_REQUEST["supid"])->and_where("status","<>","I");
		$dish->status = $status;
		$dish->save_all();
		$dish_item = ORM::factory("dishItem")->where("supplierid","=",$_REQUEST["supid"])->and_where("status","<>","I");
		$dish_item->status = $status;
		$dish_item->save_all();
		exit($msg);
	}

	private function updateStatus($result_supplier,$status){
		$updateAction = false;
		if ($result_supplier){
			$updateAction = true;
			return $updateAction;
		}
		return false;
	}

	private function baseInfo($view){
		$supplier_category = ORM::factory("supplierCategory");
		$result_supplier_category = $supplier_category->where("status","=","A")->find_all();
		$supplier = ORM::factory("supplier");
		$count = $supplier->where("status","<>","I")->count_all();
		$pagination = Pagination::factory(array(
	  		'total_items'    => $count,
		));
		$result_supplier = $supplier->where("status","<>","I")->order_by('status','DESC')->order_by('id','DESC')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();

		$view->set_global('result_supplier', $result_supplier);
		$view->set_global('result_supplier_category', $result_supplier_category);

		$page_links = $pagination->render();
		$view->set_global('page_links', $page_links);
		$view->set_global('current_page', 1);
	}

	public function action_copy(){
		$supplierid = $_POST["supid"];

		//1.copy supplier
		$supplier = ORM::factory("supplier",$supplierid);
		$new_supplier = ORM::factory("supplier");
		$new_supplier = $this->cloneEntity($supplier,$new_supplier);
		$new_supplier->jointime = Date::formatted_time();

		$copyStatus = true;
		if ($new_supplier->save()){
			//2.copy dish category
			$result_dish_category = ORM::factory("dishCategory")->where("supplierid","=",$supplierid)->and_where("status","<>","I")->find_all()->as_array();
			foreach ($result_dish_category as $dish_category) {
				$dish_category_id = $dish_category->id;
				$new_dish_category = ORM::factory("dishCategory");
				$new_dish_category = $this->cloneEntity($dish_category,$new_dish_category);
				$new_dish_category->supplierid = $new_supplier->id;
				$new_dish_category->copyfrom = $dish_category_id;
				if ($new_dish_category->save()){
					//3.copy dish
					$result_dish = ORM::factory("dish")->where("dish_cate_id","=",$dish_category_id)->and_where("status","<>","I")->find_all()->as_array();
					foreach ($result_dish as $dish){
						$dish_id = $dish->id;
						$new_dish = ORM::factory("dish");
						$new_dish = $this->cloneEntity($dish,$new_dish);
						$new_dish->dish_cate_id = $new_dish_category->id;
						$new_dish->supplierid = $new_supplier->id;
						$new_dish->copyfrom = $dish->id;
						if($new_dish->save()){
							$result_dish_item = ORM::factory("dishItem")->where("dishid","=",$dish_id)->and_where("status","<>","I")->find_all()->as_array();
							foreach ($result_dish_item as $dish_item){
								$new_dish_item = ORM::factory("dishItem");
								$new_dish_item = $this->cloneEntity($dish_item,$new_dish_item);
								$new_dish_item->dishid = $new_dish->id;
								$new_dish_item->dishcateid = $new_dish_category->id;
								$new_dish_item->supplierid = $new_supplier->id;
								$new_dish_item->copyfrom = $dish_item->id;
								if($new_dish_item->save()){
									
								}else{
									$copyStatus = false;
								}
							}
						}else{
							$copyStatus = false;
						}
					}
				}else{
					$copyStatus = false;
				}
			}
		}else{
			$copyStatus = false;
		}
		$returnMsg = "操作成功";
		if(!$copyStatus){
			$returnMsg = "操作失败";
		}
		$view = View::factory("pages/bo/common/message_dialog");
		$view->message = $returnMsg;
		exit($view->render());
	}

	private function cloneEntity($srcEntity,$newEntity){
		foreach ($srcEntity->list_columns() as $col) {
			$newEntity->__set($col["column_name"],$srcEntity->__get($col["column_name"]));
		}
		$newEntity->id = null;
		return $newEntity;
	}
}
